<?php

namespace app\api\model;

use think\Model;
use traits\model\SoftDelete;

class Gardens extends Model
{
    use SoftDelete;
    protected $deleteTime = 'deletetime';
    protected $hidden = ['deletetime', 'createtime', 'updatetime'];

    public function actionImages()
    {
        return $this->hasMany(GardenImages::class, 'garden_id', 'id')->where('type', 'action');
    }
    public function summaryImages()
    {
        return $this->hasMany(GardenImages::class, 'garden_id', 'id')->where('type', 'summary');
    }

    public static function getByStudent($student_id, $date)
    {
        $startTime = strtotime($date);
        $endTime = $startTime + 86399;
        return self::with(['actionImages', 'summaryImages'])->where('student_id', $student_id)->whereTime('createtime', [$startTime, $endTime])->find();
    }

    public static function createGarden($student_id, $data)
    {
        return self::create([
            'student_id' => $student_id,
            'eating' => $data['eating'],
            'water' => $data['water'],
            'washroom' => $data['washroom'],
            'action' => $data['action'] ?? '',
            'summary' => $data['summary'] ?? ''
        ]);
    }

    public static function updateGarden($garden_id, $data)
    {
        return self::where('id', $garden_id)->update([
            'eating' => $data['eating'],
            'water' => $data['water'],
            'washroom' => $data['washroom'],
            'action' => $data['action'] ?? '',
            'summary' => $data['summary'] ?? ''
        ]);
    }
}
